package c.j.a.c.g$g;

import android.text.TextUtils;
import c.j.a.c.g.y;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONObject;

/* compiled from: ModelManager */
public class d {
    public final AtomicBoolean a = new AtomicBoolean(false);
    public final ConcurrentHashMap<Long, c.j.a.a.a.c.d> b = new ConcurrentHashMap();
    public final ConcurrentHashMap<Long, c.j.a.a.a.c.c> c = new ConcurrentHashMap();
    public final ConcurrentHashMap<Long, c.j.a.a.a.c.b> d = new ConcurrentHashMap();
    public final ConcurrentHashMap<Long, c.j.a.b.a.c.a> e = new ConcurrentHashMap();

    /* compiled from: ModelManager */
    public class a implements Runnable {
        public void run() {
            if (d.this.a.compareAndSet(false, true)) {
                ConcurrentHashMap concurrentHashMap = d.this.e;
                ConcurrentHashMap concurrentHashMap2 = new ConcurrentHashMap();
                Map all = y.a().getSharedPreferences("sp_ad_download_event", 0).getAll();
                if (all != null) {
                    for (Entry entry : all.entrySet()) {
                        if (entry.getValue() != null) {
                            try {
                                long longValue = Long.valueOf((String) entry.getKey()).longValue();
                                c.j.a.b.a.c.a d = c.j.a.b.a.c.a.d(new JSONObject(String.valueOf(entry.getValue())));
                                if (longValue > 0 && d != null) {
                                    concurrentHashMap2.put(Long.valueOf(longValue), d);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                concurrentHashMap.putAll(concurrentHashMap2);
            }
        }
    }

    /* compiled from: ModelManager */
    public static class b {
        public long a;
        public c.j.a.a.a.c.d b;
        public c.j.a.a.a.c.c c;
        public c.j.a.a.a.c.b d;

        public boolean a() {
            return this.a <= 0 || this.b == null || this.c == null || this.d == null;
        }

        public b(long j, c.j.a.a.a.c.d dVar, c.j.a.a.a.c.c cVar, c.j.a.a.a.c.b bVar) {
            this.a = j;
            this.b = dVar;
            this.c = cVar;
            this.d = bVar;
        }
    }

    /* compiled from: ModelManager */
    public static class c {
        public static d a = new d(null);
    }

    public d(a aVar) {
    }

    public c.j.a.a.a.c.d a(long j) {
        return (c.j.a.a.a.c.d) this.b.get(Long.valueOf(j));
    }

    public c.j.a.b.a.c.a b(com.ss.android.socialbase.downloader.g.c cVar) {
        if (cVar == null) {
            return null;
        }
        if (!TextUtils.isEmpty(cVar.h)) {
            try {
                long b = c.j.a.c.q.d.b(new JSONObject(cVar.h), "extra");
                if (b > 0) {
                    for (c.j.a.b.a.c.a aVar : this.e.values()) {
                        if (aVar != null) {
                            if (aVar.a == b) {
                                return aVar;
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (c.j.a.b.a.c.a aVar2 : this.e.values()) {
            if (aVar2 != null) {
                if (aVar2.q == cVar.g0()) {
                    return aVar2;
                }
            }
        }
        for (c.j.a.b.a.c.a aVar22 : this.e.values()) {
            if (aVar22 != null) {
                if (TextUtils.equals(aVar22.f, cVar.d)) {
                    return aVar22;
                }
            }
        }
        return null;
    }

    public c.j.a.b.a.c.a c(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        for (c.j.a.b.a.c.a aVar : this.e.values()) {
            if (aVar != null && str.equals(aVar.e)) {
                return aVar;
            }
        }
        return null;
    }

    public Map<Long, c.j.a.b.a.c.a> d(String str, String str2) {
        HashMap hashMap = new HashMap();
        if (!(TextUtils.isEmpty(str) || TextUtils.isEmpty(str2))) {
            for (c.j.a.b.a.c.a aVar : this.e.values()) {
                if (aVar != null && TextUtils.equals(aVar.f, str)) {
                    aVar.e = str2;
                    hashMap.put(Long.valueOf(aVar.a), aVar);
                }
            }
        }
        return hashMap;
    }

    public synchronized void e(c.j.a.b.a.c.a aVar) {
        this.e.put(Long.valueOf(aVar.a), aVar);
        c.j.a.c.g$g.g.b.a.a(aVar);
    }

    public void f() {
        c.j.a.c.o.a.a.b(new a(), true);
    }

    public c.j.a.b.a.c.a g(long j) {
        return (c.j.a.b.a.c.a) this.e.get(Long.valueOf(j));
    }

    public b h(long j) {
        b bVar = new b();
        bVar.a = j;
        bVar.b = (c.j.a.a.a.c.d) this.b.get(Long.valueOf(j));
        bVar.c = (c.j.a.a.a.c.c) this.c.get(Long.valueOf(j));
        c.j.a.a.a.c.b bVar2 = (c.j.a.a.a.c.b) this.d.get(Long.valueOf(j));
        bVar.d = bVar2;
        if (bVar2 == null) {
            bVar.d = new c.j.a.b.a.a.a();
        }
        return bVar;
    }
}
